<?php


use Phinx\Migration\AbstractMigration;

class InitMigration extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    addCustomColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Any other destructive changes will result in an error when trying to
     * rollback the migration.
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $user = $this->table('user', ['engine'=>'MyISAM','comment'=>'管理员表']);
        $user->addColumn('username', 'string', ['limit'=>64, 'comment'=>'用户名'])
            ->addColumn('password', 'string', ['limit'=>128, 'comment'=>'密码'])
            ->addColumn('slat', 'string', ['limit'=>32, 'comment'=>'slat'])
            ->addColumn('ins_time', 'timestamp', ['default'=>'CURRENT_TIMESTAMP']);
        $user->create();

        /**
         * 数据源表，应该也是一个数据量不会很大的表，类型直接用字符串表表示
         * url 给只需要 url地址的资源使用比如rss
         * target 给还需要一个名字的 比如 wechat
         * extinfo 保存额外的信息
         */
        $resource = $this->table('resource', ['engine'=>'MyISAM','comment'=>'数据源']);
        $resource->addColumn('type', 'string', ['limit'=>32, 'comment'=>'数据源类型:wechat,rss'])
            ->addColumn('name', 'string', ['limit'=>64, 'comment'=>'数据源名字'])
            ->addColumn('url', 'string', ['limit'=>256, 'null'=>true, 'comment'=>'url地址'])
            ->addColumn('target', 'string', ['limit'=>256, 'null'=>true, 'comment'=>'目标:比如公众号名字等'])
            ->addColumn('extinfo', 'string', ['limit'=>1024, 'null'=>true, 'comment'=>'额外的信息:比如数据提取规则等'])
            ->addColumn('frequency', 'integer', ['default'=>10, 'comment'=>'采集频率'])
            ->addColumn('message_count', 'integer', ['default'=>0, 'comment'=>'采集到的信息数'])
            ->addColumn('last_time', 'datetime', ['null'=>true, 'comment'=>'最后一次采集时间'])
            ->addColumn('user_id', 'integer', ['comment'=>'User ID'])
            ->addForeignKey('user_id', 'user', 'id', ['delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'])
            ->addColumn('ins_time', 'timestamp', ['default'=>'CURRENT_TIMESTAMP']);
        $resource->create();

        $body = $this->table('body', ['engine'=>'MyISAM','comment'=>'消息正文表']);
        $body->addColumn('content', 'text', ['comment'=>'内容'])
            ->addColumn('ins_time', 'timestamp', ['default'=>'CURRENT_TIMESTAMP']);
        $body->create();

        /**
         * 内容表，只保存摘要,相关访问信息，具体正文保存在额外的表中
         * 考虑到数据量大小，每天信息量，应该不多于10篇，一年3600，十年才上万，直接使用id当主键
         */
        $message = $this->table('message', ['engine'=>'MyISAM','comment'=>'消息表']);
        $message->addColumn('resource_id', 'integer', ['comment'=>'resource ID'])
            ->addForeignKey('resource_id', 'resource', 'id', ['delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'])
            ->addColumn('hashkey', 'string', ['limit'=>32, 'comment'=>'消息hash,唯一标示'])
            ->addColumn('body_id', 'integer', ['comment'=>'resource ID'])
            ->addForeignKey('body_id', 'body', 'id', ['delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'])
            ->addColumn('message_url', 'string', ['null'=>true, 'limit'=>512, 'comment'=>'资源链接地址'])
            ->addColumn('title', 'string', ['null'=>true, 'limit'=>256, 'comment'=>'资源链接地址'])
            ->addColumn('abstract', 'string', ['null'=>true, 'limit'=>256, 'comment'=>'资源摘要'])
            ->addColumn('pic', 'string', ['null'=>true, 'limit'=>256, 'comment'=>'图片'])
            ->addColumn('thumbnail', 'string', ['null'=>true, 'limit'=>256, 'comment'=>'缩略图'])
            ->addColumn('pub_date', 'datetime', ['null'=>true, 'comment'=>'rss文章发布时间'])
            ->addColumn('ispost', 'boolean', ['default'=>false, 'comment'=>'是否已经推送'])
            ->addColumn('post_date', 'timestamp', ['null'=>true, 'comment'=>'文章推送时间'])
            ->addColumn('author', 'string', ['null'=>true, 'comment'=>'文章作者'])
            ->addColumn('category', 'string', ['null'=>true, 'comment'=>'文章分类'])
            ->addColumn('vist_times', 'integer', ['default'=>0, 'comment'=>'访问次数'])
            ->addColumn('tele_likes', 'integer', ['default'=>0, 'comment'=>'电报中like次数'])
            ->addColumn('tele_unlikes', 'integer', ['default'=>0, 'comment'=>'电报中unlike次数'])
            ->addColumn('ins_time', 'timestamp', ['default'=>'CURRENT_TIMESTAMP']);
        $message->create();

        /**
         * 日志相关都可以存文件日志，不用存在数据库
         */
        $collectlog = $this->table('collectlog', ['engine'=>'MyISAM','comment'=>'消息采集日志']);
        $collectlog->addColumn('resource_id', 'string', ['limit'=>256, 'comment'=>'群组或频道id'])
            ->addColumn('url', 'string', ['limit'=>256, 'comment'=>'url'])
            ->addColumn('info_count', 'integer', ['default'=>0, 'comment'=>'采集到的信息数'])
            ->addColumn('valid_count', 'integer', ['default'=>0, 'comment'=>'采集到的有效信息数'])
            ->addColumn('status', 'boolean', ['default'=>true, 'comment'=>'是否采集成功'])
            ->addColumn('ins_time', 'timestamp', ['default'=>'CURRENT_TIMESTAMP']);
        $collectlog->create();

        /**
         * 目的地，推送到的群组，或频道
         */
        $destination = $this->table('destination', ['engine'=>'MyISAM','comment'=>'消息传递目的地']);
        $destination->addColumn('name', 'string', ['limit'=>64, 'comment'=>'名字'])
            ->addColumn('type', 'string', ['limit'=>20, 'comment'=>'类型'])
            ->addColumn('dest', 'string', ['limit'=>256, 'comment'=>'群组或频道id'])
            ->addColumn('extinfo', 'string', ['limit'=>256, 'comment'=>'额外信息'])
            ->addColumn('post_times', 'integer', ['default'=>0, 'comment'=>'频道中推送的次数'])
            ->addColumn('status', 'boolean', ['default'=>true, 'comment'=>'是否开启推送'])
            ->addColumn('ins_time', 'timestamp', ['default'=>'CURRENT_TIMESTAMP']);
        $destination->create();

        /**
         * 推送日志， 日志相关都可以存文件日志，不用存在数据库
         */
        $postlog = $this->table('postlog', ['engine'=>'MyISAM','comment'=>'发送日志']);
        $postlog->addColumn('contents_id', 'integer', ['comment'=>'内容id'])
            ->addForeignKey('contents_id', 'contents', 'id', ['delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'])
            ->addColumn('destination_id', 'integer', ['comment'=>'目的id'])
            ->addForeignKey('destination_id', 'destination', 'id', ['delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'])
            ->addColumn('success', 'boolean', ['default'=>false, 'comment'=>'是否已经推送成功'])
            ->addColumn('ins_time', 'timestamp', ['default'=>'CURRENT_TIMESTAMP']);
        $postlog->create();

        /**
         * 系统错误日志， 日志相关都可以存文件日志，不用存在数据库
         */
        $errorlog = $this->table('errorlog', ['engine'=>'MyISAM','comment'=>'脚本执行异常日志']);
        $errorlog->addColumn('info', 'string', ['limit'=>256, 'comment'=>'日志信息'])
            ->addColumn('ins_time', 'timestamp', ['default'=>'CURRENT_TIMESTAMP']);
        $errorlog->create();
    }
}
